package com.jjc.android.dialog

import android.content.Context
import android.text.method.LinkMovementMethod
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.jjc.android.Constants
import com.jjc.android.GlobalManager
import com.jjc.android.R
import com.jjc.android.UserManager
import com.jjc.android.activity.BrowserActivity
import com.jjc.android.util.ComplexSpannableStringUtils
import com.jjc.android.util.NoRefCopySpan
import com.jjc.android.util.getSpu
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils


class PrivacyDialog(context: Context, var action:((Boolean) -> Unit)? = null) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_privacy_dialog
    }

    override fun onCreate() {
        super.onCreate()
//        为了更好地保障您的合法权益，请您阅读并同意《用户协议》和《隐私政策》
        val webTextView:TextView = findViewById(R.id.contentTxt)
        webTextView.movementMethod = LinkMovementMethod.getInstance()
        webTextView.text = ComplexSpannableStringUtils.getBuilder(context.getString(R.string.ag_hint_1_txt))
            .append(context.getString(R.string.ag_hint_2_txt))
            .setForegroundColor(ContextCompat.getColor(context,R.color.colorPrimary))
            .setClickSpan(NoRefCopySpan{
                BrowserActivity.start(context, title = context.getString(R.string.user_ag_txt), pageData = GlobalManager.getUserAgreementContent())
            })
            .append(context.getString(R.string.and_txt))
            .append(context.getString(R.string.ag_hint_3_txt))
            .setForegroundColor(ContextCompat.getColor(context,R.color.colorPrimary))
            .setClickSpan(NoRefCopySpan{
                BrowserActivity.start(context, title = context.getString(R.string.privacy_ag_txt), pageData = GlobalManager.getPrivacyPolicyContent())
            })
            .create()

        val okBtn:TextView = findViewById(R.id.okBtn)
        okBtn.setOnClickListener {
            dismiss()
            UserManager.isPopuAgreement = true
            action?.invoke(true)

        }
        val cancleBtn:TextView = findViewById(R.id.cancelBtn)
        cancleBtn.setOnClickListener {
            action?.invoke(false)
            dismiss()
        }

    }

    override fun getPopupWidth(): Int {
        return (XPopupUtils.getScreenWidth(context)*0.9f).toInt()
    }

    override fun getPopupHeight(): Int {
        return ViewGroup.LayoutParams.WRAP_CONTENT
    }

}